package com.example.computer_exam_training_sys.controller;
import com.example.computer_exam_training_sys.mapper.KnowledgePointMapper;
import com.example.computer_exam_training_sys.service.UserProgressService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userProgress")
public class UserProgressController {

    @Autowired
    private UserProgressService userProgressService;

    @Autowired
    private KnowledgePointMapper knowledgePointMapper;

    // 标记完成
    @PostMapping("/complete-point")
    public Map<String, String> markPointCompleted(@RequestBody Map<String, Integer> payload) {
        Integer userId = payload.get("userId");
        Integer pointId = payload.get("pointId");
        Integer courseId = payload.get("courseId");

        userProgressService.markPointCompleted(userId, pointId, courseId);

        Map<String, String> result = new HashMap<>();
        result.put("message", "Marked as completed");
        return result;
    }

    // 获取进度
    @GetMapping("/course-progress")
    public Map<String, Object> getCourseProgress(@RequestParam Integer userId, @RequestParam Integer courseId) {
        int completed = userProgressService.countCompletedPoints(userId, courseId);
        int total = knowledgePointMapper.countByCourseId(courseId);

        double progress = total == 0 ? 0.0 : completed * 100.0 / total;

        Map<String, Object> result = new HashMap<>();
        result.put("completedCount", completed);
        result.put("totalCount", total);
        result.put("progress", progress);
        return result;
    }
    @GetMapping("/completed-points")
    public List<Integer> getCompletedPoints(@RequestParam Integer userId, @RequestParam Integer courseId) {
        return userProgressService.getCompletedPointIds(userId, courseId);
    }
    @PostMapping("/batch-status")
    public Map<String, Boolean> getBatchStatus(@RequestBody BatchStatusRequest request) {
        Map<String, Boolean> statusMap = new HashMap<>();
        for (Integer pointId : request.getPointIds()) {
            boolean completed = userProgressService.isPointCompleted(request.getUserId(), pointId);
            statusMap.put(pointId.toString(), completed);
        }
        return statusMap;
    }

    @Data
    public static class BatchStatusRequest {
        private Integer userId;
        private Integer courseId;
        private List<Integer> pointIds;
    }
}
